import sendRequest from "./request.js";

const getQrCode = param => {
  return new Promise((resolve, reject) => {
    sendRequest("https://zhuanti.chebada.com/activityapiv2/CommonActivity", {
      data: param,
      serviceName: "getminaqrcode",
      hideLoading: true
    }).then(res => {
      if (res) {
        resolve(res.codeImageStr);
      } else {
        reject("error");
      }
    });
  });
};

// const createImg = url => {
//   return new Promise((resolve, reject) => {
//     const qrImg = new Image();
//     qrImg.crossOrigin = "Anonymous";
//     qrImg.onload = () => {
//       resolve(qrImg);
//     };
//     qrImg.onerror = () => {
//       reject("生成二维码失败");
//     };
//     qrImg.src = url;
//   });
// };

async function drawCanvas(param) {
  try {
    const qrCode = await getQrCode(param);
    return "data:image/png;base64," + qrCode;
  } catch (e) {
    console.log(e);
  }
}

export default drawCanvas;
